//
//  LCMineViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/20.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCMineViewController.h"
#import "LCMineService.h"
#import "LCUserModel.h"
#import "LCCouponService.h"
#import "LCCouponAdvertModel.h"

#import "LCMineAdvertTableViewCell.h"
#import "LCMineBindTableViewCell.h"
#import "LCMineToolTableViewCell.h"

#import "LCSettingViewController.h"
#import "XWXAllXieYiController.h"
#import "LCMineInfoViewController.h"

#import "LCUserAmountViewController.h"
#import "LCUserChsNumberViewController.h"
#import "LCUserRewardTotalViewController.h"

#import "LCWithdrawalViewController.h"
#import "LCBDWeChatViewController.h"
#import "LCBDAliPayViewController.h"
#import "LCPhoneHaveViewController.h"

#import "LCInviteFriendViewController.h"
#import "LCMyTeamViewController.h"
#import "LCRankingViewController.h"
#import "LCKeFuCenterViewController.h"
#import "LCFeedbackViewController.h"
#import "LCAboutUsViewController.h"

@interface LCMineViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIView *headView;
//头像
@property (weak, nonatomic) IBOutlet UIImageView *thumbView;
//昵称
@property (weak, nonatomic) IBOutlet UILabel *nickNameLb;
//邀请码
@property (weak, nonatomic) IBOutlet UILabel *shareIdLb;
//今日收益
@property (weak, nonatomic) IBOutlet UILabel *todayrewordLb;
//账户余额
@property (weak, nonatomic) IBOutlet UILabel *userAmountLb;
//邀请人数
@property (weak, nonatomic) IBOutlet UILabel *chsnumberLb;
//累计收益
@property (weak, nonatomic) IBOutlet UILabel *rewardtotalLb;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic , strong) LCUserModel *dataMode;

@property (nonatomic, strong)NSArray * toolArr;

@property (nonatomic, strong)NSMutableArray * advertArr;

@end

@implementation LCMineViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    CGRect headFrame = self.headView.frame;
    headFrame.size.height = 204*proW+77;
    self.headView.frame = headFrame;
    [LCNotiCenter addObserver:self selector:@selector(loadReloadData) name:@"LoadUserInfoNotification" object:nil];
    [self loadToolData];
    [self loadAdvertData];
    [self setTableViewRefresh];
}

-(void)loadReloadData {
    [self loadUserData:NO];
}

-(void)setTableViewRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadUserData:YES];
    }];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
}

-(void)loadToolData {
    self.toolArr = @[@{@"icon":@"lc_mine_tool_yqhy",@"name":@"邀请好友"},
                     @{@"icon":@"lc_mine_tool_wdtd",@"name":@"我的团队"},
                     @{@"icon":@"lc_mine_tool_yhph",@"name":@"用户排行"},
                     @{@"icon":@"lc_mine_tool_kfzx",@"name":@"客服中心"},
                     @{@"icon":@"lc_mine_tool_tsfk",@"name":@"投诉反馈"},
                     @{@"icon":@"lc_mine_tool_gywm",@"name":@"关于我们"},
                     @{@"icon":@"lc_mine_tool_bbgx",@"name":@"更新版本"}];
}

-(NSArray *)toolArr {
    if (!_toolArr) {
        _toolArr = [NSArray array];
    }
    return _toolArr;
}

-(void)loadAdvertData {
    [LCCouponService getAdvertWithAdvertname:@"个人中心广告图1" Block:^(BOOL isSuccess, NSArray<LCCouponAdvertModel *> *list) {
        if (isSuccess) {
            [self.advertArr replaceObjectAtIndex:0 withObject:list.firstObject];
            [self.tableView reloadData];
        }
    }];
    [LCCouponService getAdvertWithAdvertname:@"个人中心广告图2" Block:^(BOOL isSuccess, NSArray<LCCouponAdvertModel *> *list) {
        if (isSuccess) {
            [self.advertArr replaceObjectAtIndex:1 withObject:list.firstObject];
            [self.tableView reloadData];
        }
    }];
}

- (NSMutableArray *)advertArr {
    if (!_advertArr) {
        _advertArr = [NSMutableArray arrayWithArray:@[@"",@""]];
    }
    return _advertArr;
}


- (void)loadUserData:(BOOL)showTS {
    [LCMineService getUserInfoWithShowError:showTS Block:^(BOOL isSuccess, LCUserModel *userInfo) {
        [self.tableView.mj_header endRefreshing];
        if (isSuccess) {
            self.dataMode = userInfo;
            [self setHeadViewUI];
            [self.tableView reloadData];
        }
    }];
}

-(void)setHeadViewUI {
    [self.thumbView sd_setImageWithURL:[LCFunction XWX_URLImg:self.dataMode.userthumb] placeholderImage:[UIImage imageNamed:@"lc_mine_user_head"]];
    self.nickNameLb.text = self.dataMode.nickname;
    self.shareIdLb.text = [NSString stringWithFormat:@"用户ID：%@",self.dataMode.userid];
    [self.thumbView whenTapped:^{
        [self pushToControllerWithName:@"个人资料"];
    }];
    [self.nickNameLb whenTapped:^{
        [self pushToControllerWithName:@"个人资料"];
    }];
    
    self.todayrewordLb.text = self.dataMode.todayreword;
    self.userAmountLb.text = self.dataMode.assets.UserAmount.value;
    self.chsnumberLb.text = self.dataMode.chsnumber;
    self.rewardtotalLb.text = self.dataMode.rewardtotal;
}

- (IBAction)shareCodeCopyAction:(id)sender {
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
    NSString*string = kFORMAT(@"U%@",self.dataMode.userid);
    [pab setString:string];
    if(pab == nil) {
        [self showErrorText:@"复制失败"];
    }else{
        [self showSuccessText:@"复制成功"];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 3;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0: {
            return 75 * proW + 14;
        } break;
        case 1: {
            return 127;
        } break;
        default: {
            return 200;
        } break;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (indexPath.row) {
        case 0: {
            LCMineAdvertTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LCMineAdvertTableViewCell"];
            cell.advertArr = self.advertArr;
            cell.clickAction = ^(NSString * _Nonnull name) {
                [self pushToControllerWithName:name];
            };
            return cell;
        } break;
        case 1: {
            LCMineBindTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LCMineBindTableViewCell"];
            cell.clickAction = ^(NSString * _Nonnull name) {
                [self pushToControllerWithName:name];
            };
            return cell;
        } break;
        default: {
            LCMineToolTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LCMineToolTableViewCell"];
            cell.toolArr = self.toolArr;
            cell.clickAction = ^(NSString * _Nonnull name) {
                [self pushToControllerWithName:name];
            };
            return cell;
        } break;
    }
}

- (IBAction)setBtnAction:(UIButton *)sender {
    [self pushToControllerWithName:@"设置"];
}

- (IBAction)amountBtnAction:(id)sender {
    [self pushToControllerWithName:@"账户余额"];
}

- (IBAction)chsNumberBtnAction:(id)sender {
//    [self pushToControllerWithName:@"邀请人数"];
}

- (IBAction)rewardTotalBtnAction:(id)sender {
//    [self pushToControllerWithName:@"累计收益"];
}

-(void)pushToControllerWithName:(NSString *)name {
    if ([NSString urlValidation:name]) {
        XWXAllXieYiController *webVC = [XWXAllXieYiController new];
        webVC.urlStr = name;
        webVC.isPushFrom = YES;
        webVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:webVC animated:YES];
    } else if ([name isEqualToString:@"设置"]) {
        LCSettingViewController * pushVC = [LCSettingViewController xx_instantiateFromStoryboardNamed:@"Mine"];
        pushVC.dataMode = self.dataMode;
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"个人资料"]) {
        LCMineInfoViewController * pushVC = [LCMineInfoViewController xx_instantiateFromStoryboardNamed:@"Mine"];
        pushVC.dataMode = self.dataMode;
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"账户余额"]) {
        LCUserAmountViewController * pushVC = [LCUserAmountViewController xx_instantiateFromStoryboardNamed:@"Mine"];
        pushVC.amount = self.dataMode.assets.UserAmount;
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"邀请人数"]) {
        LCUserChsNumberViewController * pushVC = [LCUserChsNumberViewController xx_instantiateFromStoryboardNamed:@"Mine"];
        pushVC.chsnumber = self.dataMode.chsnumber;
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"累计收益"]) {
        LCUserRewardTotalViewController * pushVC = [LCUserRewardTotalViewController xx_instantiateFromStoryboardNamed:@"Mine"];
        pushVC.rewardtotal = self.dataMode.rewardtotal;
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"余额提现"]) {
        LCWithdrawalViewController * pushVC = [LCWithdrawalViewController xx_instantiateFromStoryboardNamed:@"Mine"];
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"微信绑定"]) {
        LCBDWeChatViewController * pushVC = [LCBDWeChatViewController xx_instantiateFromStoryboardNamed:@"MineBind"];
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"支付宝绑定"]) {
        LCBDAliPayViewController * pushVC = [LCBDAliPayViewController xx_instantiateFromStoryboardNamed:@"MineBind"];
        pushVC.alipay = self.dataMode.alipay;
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"手机号绑定"]) {
        LCPhoneHaveViewController * pushVC = [LCPhoneHaveViewController xx_instantiateFromStoryboardNamed:@"MineBind"];
        pushVC.usermobile = self.dataMode.usermobile;
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"邀请好友"]) {
        LCInviteFriendViewController * pushVC = [LCInviteFriendViewController xx_instantiateFromStoryboardNamed:@"Invite"];
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"领现金好礼"]) {
        self.tabBarController.selectedIndex = 1;
    } else if ([name isEqualToString:@"我的团队"]) {
        LCMyTeamViewController * pushVC = [LCMyTeamViewController xx_instantiateFromStoryboardNamed:@"MineTool"];
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"用户排行"]) {
        LCRankingViewController * pushVC = [LCRankingViewController xx_instantiateFromStoryboardNamed:@"MineTool"];
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"客服中心"]) {
        LCKeFuCenterViewController * pushVC = [LCKeFuCenterViewController xx_instantiateFromStoryboardNamed:@"MineTool"];
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"投诉反馈"]) {
        LCFeedbackViewController * pushVC = [LCFeedbackViewController xx_instantiateFromStoryboardNamed:@"MineTool"];
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"关于我们"]) {
        LCAboutUsViewController * pushVC = [LCAboutUsViewController xx_instantiateFromStoryboardNamed:@"MineTool"];
        [self.navigationController pushViewController:pushVC animated:YES];
    } else if ([name isEqualToString:@"更新版本"]) {
        [LCMineService checkVersionWirhBlock:^(BOOL isSuccess, BOOL hasNewV) {
            if (hasNewV) {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"升级提示" message:@"有新版本可用，请前往app Store下载哦！" preferredStyle:UIAlertControllerStyleAlert];
                [alert addAction:[UIAlertAction actionWithTitle:@"关闭" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                }]];
                [self presentViewController:alert animated:YES completion:nil];
            }
        }];
    }
}

@end
